package com.yunhui.admin.web;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yunhui.admin.AdminUserContext;

import com.yunhui.mapper.YCAdminMapper;
import com.yunhui.util.Des3;
import com.yunhui.util.WGHStringUtils;


/**
 * 修改密码
 * @author Administrator
 *
 */
@Controller
@RequestMapping("${yunhui.admin.urlContext}")
public class UpdatePasswordController {
	
	
	@Autowired
	private YCAdminMapper yCAdminMapper;
	
	@RequestMapping("/myaccount/checkpassword")
	@ResponseBody
	public Object checkPassword(HttpServletRequest request,HttpServletResponse response) {
		Map<String, Object> resultMap = new HashMap<>();
		String  password  =  request.getParameter("password");
		
		if(WGHStringUtils.isBlank(password)) {
			resultMap.put("message", "密码不能为空");
			resultMap.put("success", false);
			return resultMap;
		}
		//获取当前登录用户的账号
		String account =AdminUserContext.getAccount();
		
		String endPassword = Des3.encrypt(password);
		
		Map<String, Object> paramMap = new HashMap<>();
		paramMap.put("account", account);
		paramMap.put("password", endPassword);
		long checkNumber = yCAdminMapper.selectYCAdminByAccountByIDAndPassword(paramMap);
		if(checkNumber==1) {
			resultMap.put("message", "验证成功");
			resultMap.put("success",true);
			
			return resultMap;
		}
		resultMap.put("message", "验证失败");
		resultMap.put("success", false);
		return resultMap;
	}
	
	/**
	 * 修改密码返回ajax
	 * @param request
	 * @return
	 */
	@RequestMapping("/myaccount/updatepassword")
	@ResponseBody
	public Object doUpdatePassword(HttpServletRequest request) {
		Map<String, Object> resultMap = new HashMap<>();
		try {
			//获取当前登录用户的账号
			String account =AdminUserContext.getAccount();
			String  password  =  request.getParameter("password");
			if(WGHStringUtils.isBlank(password)) {
				resultMap.put("message", "密码不能为空");
				resultMap.put("success", false);
				return resultMap;
			}
			Map<String, Object> paramMap = new HashMap<>();
			paramMap.put("account", account);
			paramMap.put("password", Des3.encrypt(password));

			yCAdminMapper.updatePasswordByAccount(paramMap);
			resultMap.put("message","密码修改成功即将退出登录" );
			resultMap.put("success",true );
		} catch (Exception e) {
			resultMap.put("message","密码修改失败了" );
			resultMap.put("success",false );
		}
		
		return resultMap;
	}
}
